package com.qf.dao.impl;


import com.qf.dao.UserDao;
import com.qf.pojo.TbUser;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.Date;

/*
 *@Description:
 *@author:刘哥
 *@Date:16:47 2022/8/13
 */
public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public TbUser selectByName(String username) {
        String sql="select * from tb_user where username=?";
        try {
            return queryRunner.query(sql, new BeanHandler<>(TbUser.class),username);
        } catch (SQLException e) {
           throw new RuntimeException(e);
        }
    }

    @Override
    public void insertUser(TbUser user) {
        String sql="insert into tb_user(username,password,email,gender,flag,role,code) value (?,?,?,?,?,?,?)";
        Object[] param={user.getUsername(),user.getPassword(),user.getEmail(),user.getGender(),user.getFlag(),user.getRole(),user.getCode()};
        try {
            queryRunner.update(sql, param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateFlag(String emali, String code) {
        String sql="update tb_user set flag=1 where email=? and code=? and flag=0";
        try {
            return queryRunner.update(sql,emali,code);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void upDateLoginTime(Long id) {
        String sql="update tb_user set lastlogintime=currentlogintime,currentlogintime=? where id=? ";
        Object[] param={new Date(),id};
        try {
            queryRunner.update(sql, param);
        } catch (SQLException e) {
        throw new RuntimeException(e);
        }
    }
}
